#include "utils/FileManager.h"
#include <QDir>
#include <QFile>
#include <QDirIterator>

bool FileManager::copyDirectory(const QString &src, const QString &dest)
{
    QDir srcDir(src);
    if (!srcDir.exists()) return false;
    
    QDir destDir(dest);
    if (!destDir.exists()) {
        destDir.mkpath(".");
    }
    
    QDirIterator it(src, QDirIterator::Subdirectories);
    while (it.hasNext()) {
        QString srcPath = it.next();
        QString destPath = srcPath;
        destPath.replace(src, dest);
        
        QFileInfo info(srcPath);
        if (info.isDir()) {
            QDir().mkpath(destPath);
        } else if (info.isFile()) {
            QFile::copy(srcPath, destPath);
        }
    }
    
    return true;
}

bool FileManager::removeDirectory(const QString &path)
{
    return QDir(path).removeRecursively();
}

QStringList FileManager::findFiles(const QString &path, const QStringList &patterns)
{
    QStringList files;
    QDirIterator it(path, patterns, QDir::Files, QDirIterator::Subdirectories);
    while (it.hasNext()) {
        files << it.next();
    }
    return files;
}

bool FileManager::backupFile(const QString &filePath)
{
    return QFile::copy(filePath, filePath + ".bak");
}

bool FileManager::restoreFile(const QString &backupPath, const QString &originalPath)
{
    QFile::remove(originalPath);
    return QFile::copy(backupPath, originalPath);
}